<template>
  <div>
    <!-- 工作日历, 年和月 -->
    <div class="select-box">
      <el-select
        v-model="currentYear"
        size="small"
        style="width: 120px; margin-right: 10px"
        @change="changeDate"
      >
        <!-- 年份取给定年份的, 前五年 + 后五年 -->
        <el-option
          v-for="item in yearList"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
      <el-select
        v-model="currentMonth"
        size="small"
        style="width: 120px;"
        @change="changeDate"
      >
        <el-option v-for="item in 12" :key="item" :label="item" :value="item" />
      </el-select>
    </div>
    <el-calendar v-model="value">
      <template #dateCell="{ data ,date}">
        <div class="date-content">
          <span class="text">{{ data.day | getDay }}</span>
          <span v-if="isWeek(date)" class="rest">休</span>
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
export default {
  // 过滤器
  filters: {
    getDay(value) {
      const day = value.split('-')[2]
      return day.startsWith('0') ? day.slice(1) : day
    }
  },
  props: {
    //   开始默认日期，默认今时今日
    startDate: {
      type: Date,
      default: () => {
        return new Date()
      }
    }
  },
  data() {
    return {
      value: '',
      currentYear: '',
      currentMonth: ''
      //   weekList: [
      //     '2021-08-12',
      //     '2021-08-13',
      //     '2021-08-14',
      //     '2021-08-15',
      //     '2021-08-20',
      //     '2021-08-31'
      //   ]
    }
  },
  computed: {
    //   放在计算属性里，要是this.currentYear(响应式数据，点了就变)变了就重新计算
    yearList() {
      // Array.from第二个参数，如果指定了该参数，新数组中的每个元素会执行该回调函数。
      return Array.from(new Array(11), (item, index) => {
        return index + this.currentYear - 5
      })
    }
  },
  //   监听value,点击下月的某一日期，改变上方下拉框的值
  watch: {
    value(newVal) {
      //   console.log(newVal)
      this.currentYear = newVal.getFullYear()
      this.currentMonth = newVal.getMonth() + 1
    }
  },
  //   组件创建之初就赋值下拉框的值，根据startDate现在的时间来渲染
  created() {
    this.currentYear = this.startDate.getFullYear()
    this.currentMonth = this.startDate.getMonth() + 1
    this.value = this.startDate
    // console.log(this.value)
  },
  methods: {
    //   改变下拉框的值
    changeDate() {
      this.value = new Date(this.currentYear + '-' + this.currentMonth + '-1')
    },
    // 判断是否是周末，加'休'
    isWeek(date) {
      // Date对象getDay()方法,不是过滤器
      return date.getDay() === 6 || date.getDay() === 0
      //   return this.weekList.includes(data.day)
    }
  }
}
</script>

<style lang="scss" scoped>
.select-box {
  display: flex;
  justify-content: flex-end;
}
::v-deep .el-calendar-day {
  height: auto;
}
::v-deep
  .el-calendar-table__row
  td::v-deep
  .el-calendar-table
  tr
  td:first-child,
::v-deep .el-calendar-table__row td.prev {
  border: none;
}
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text {
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
}
::v-deep .el-calendar-table td.is-selected .text {
  background: #409eff;
  color: #fff;
  border-radius: 50%;
}
::v-deep .el-calendar__header {
  display: none;
}
</style>
